package com.lt.basic.project.application.service.admin.resource.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lt.basic.project.application.mapper.admin.resource.FileMapper;
import com.lt.basic.project.application.plugin.storage.StorageDriver;
import com.lt.basic.project.application.plugin.storage.UploadFilesVo;
import com.lt.basic.project.application.service.admin.resource.IFileService;
import com.lt.basic.project.common.core.PageResult;
import com.lt.basic.project.common.enums.ErrorEnum;
import com.lt.basic.project.common.exception.BaseException;
import com.lt.basic.project.common.util.ConvertUtils;
import com.lt.basic.project.pojo.entity.admin.resource.File;
import com.lt.basic.project.pojo.validate.admin.resource.FileCreateValidate;
import com.lt.basic.project.pojo.validate.admin.resource.FileSearchValidate;
import com.lt.basic.project.pojo.validate.admin.resource.FileUpdateValidate;
import com.lt.basic.project.pojo.validate.commons.IdValidate;
import com.lt.basic.project.pojo.validate.commons.PageValidate;
import com.lt.basic.project.pojo.vo.admin.resource.FileDetailVo;
import com.lt.basic.project.pojo.vo.admin.resource.FileListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件管理业务接口实现
 * @author LT
 * @version 1.0.0 2023-08-25
 */
@Slf4j
@Service
@Transactional
public class FileServiceImpl implements IFileService {

    @Resource
    private FileMapper fileMapper;

    /**
    * 转换VO数据
    * @param file 数据对象
    * @return FileResponse 返回Response对象
    */
    public FileDetailVo getConvertResponse(File file){
        FileDetailVo detailVo = ConvertUtils.convert(file, FileDetailVo.class);
        return detailVo;
    }

    /**
    * 根据id获取一条VO数据
    * @param id 主键参数
    * @return FileDetailVo
    */
    @Override
    public FileDetailVo detail(Long id) {
        File file = fileMapper.selectById(id);
        if(null == file) {
            throw new BaseException(ErrorEnum.DATA_ERROR.getCode(), ErrorEnum.DATA_ERROR.getMsg());
        }
        return this.getConvertResponse(file);
    }

    /**
    * 获取列表数据
    * @param searchValidate 查询条件
    * @param pageValidate 分页条件
    * @return PageResult<FileListVo> 响应分页数据
    */
    @Override
    public PageResult<FileListVo> list(PageValidate pageValidate, FileSearchValidate searchValidate) {
        //带SystemAdminRequest类的多条件查询
        LambdaQueryWrapper<File> lqw = Wrappers.lambdaQuery();
        searchValidate.query(searchValidate, lqw);
        IPage<File> iPage = fileMapper.selectPage(new Page<>(pageValidate.getPageNo(), pageValidate.getPageSize()), lqw);
        List<FileListVo> list = new ArrayList<>();
        iPage.getRecords().forEach(file -> {
            FileListVo vo = ConvertUtils.convert(this.getConvertResponse(file), FileListVo.class);
            list.add(vo);
        });
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
    * 保存
    * @param createValidate 请求参数
    */
    @Override
    public void add(FileCreateValidate createValidate) {
        File file = new File();
        file.create(createValidate);
        fileMapper.insert(file);
    }

    /**
    * 编辑
    * @param updateValidate 请求参数
    */
    @Override
    public void edit(FileUpdateValidate updateValidate) {
        File file = fileMapper.selectById(updateValidate.getId());
        if(null == file) {
            throw new BaseException(ErrorEnum.DATA_ERROR.getCode(), ErrorEnum.DATA_ERROR.getMsg());
        }
        file.modify(updateValidate);
        fileMapper.updateById(file);
    }

    /**
    * 删除数据
    * @param idValidate 请求参数
    */
    @Override
    public void del(IdValidate idValidate) {
        File file = fileMapper.selectById(idValidate.getId());
        if(null == file) {
            throw new BaseException(ErrorEnum.DATA_ERROR.getCode(), ErrorEnum.DATA_ERROR.getMsg());
        }
        file.setIsDel(true);
        fileMapper.updateById(file);
    }

    @Override
    public String upload(MultipartFile file, Integer type) {
        StorageDriver driver = new StorageDriver();
        UploadFilesVo upload = driver.upload(file, "file", type);
        FileCreateValidate createValidate = new FileCreateValidate();
        createValidate.setName(upload.getName());
        createValidate.setType(type);
        createValidate.setUrl(upload.getPath());
        this.add(createValidate);
        return upload.getPath();
    }

    private File getByUrl(String format) {
        return fileMapper.selectOne(Wrappers.lambdaQuery(File.class).eq(File::getUrl, format).eq(File::getIsDel, false).last(" limit 1"));
    }

    @Override
    public File latest(Integer contentType){
        return fileMapper.selectOne(Wrappers.lambdaQuery(File.class)
                .eq(File::getType, contentType).orderByDesc(File::getPublishTime)
                .last(" limit 1"));
    }

    @Override
    public void effective(IdValidate idValidate) {
        File file = fileMapper.selectById(idValidate.getId());
        if (null != file){
            file.setIsEffective(!file.getIsEffective());
            fileMapper.updateById(file);
        }
    }


}